﻿Imports BUS
Imports System.Globalization
Imports System.Drawing.Printing


Public Class frmHeThongPhuHop

#Region "Thuộc tính"
    Private _tongCongSuatTieuThuTrongNgay As Integer = 0
    Private _tongCongSuatPin As Integer = 0
    Private _dungLuongAcquy12V As Double = 0
    Private _dungLuongAcquy24V As Double = 0
    Private _coThietBiDeBa As Boolean = False
    Private _dienApDauRaBoKichDien As Integer = 0
    Private _functionSolar As Integer = 0
    Private _soNgaySac As Integer = 0
    Private _soNgaySuDung As Integer = 0
    Private _status As Boolean = False
    Private _tongCongSuatTieuThu1Gio As Integer = 0
#End Region

    Dim MyDataGridViewPrinter As DataGridViewPrinter

#Region "GetThuocTinh"


    Public Property DienApDauRaBoKichDien() As Integer
        Get
            Return _dienApDauRaBoKichDien
        End Get
        Set(ByVal value As Integer)
            _dienApDauRaBoKichDien = value
        End Set
    End Property
    Public Property DungLuongAcquy12V() As Double
        Get
            Return _dungLuongAcquy12V
        End Get
        Set(ByVal value As Double)
            _dungLuongAcquy12V = value
        End Set
    End Property
    Public Property DungLuongAcquy24V() As Double
        Get
            Return _dungLuongAcquy24V
        End Get
        Set(ByVal value As Double)
            _dungLuongAcquy24V = value
        End Set
    End Property
    Public Property FunctionSolar() As Integer
        Get
            Return _functionSolar
        End Get
        Set(ByVal value As Integer)
            _functionSolar = value
        End Set
    End Property
    Public Property TongCongSuatPin() As Integer
        Get
            Return _tongCongSuatPin
        End Get
        Set(ByVal value As Integer)
            _tongCongSuatPin = value
        End Set
    End Property
    Public Property TongCongSuatTieuThu1Gio() As Integer
        Get
            Return _tongCongSuatTieuThu1Gio
        End Get
        Set(ByVal value As Integer)
            _tongCongSuatTieuThu1Gio = value
        End Set
    End Property
    Public Property TongCongSuatTieuThuTrongNgay() As Integer
        Get
            Return _tongCongSuatTieuThuTrongNgay
        End Get
        Set(ByVal value As Integer)
            _tongCongSuatTieuThuTrongNgay = value
        End Set
    End Property
    Public Property Status() As Boolean
        Get
            Return _status
        End Get
        Set(ByVal value As Boolean)
            _status = value
        End Set
    End Property
    Public Property SoNgaySac() As Integer
        Get
            Return _soNgaySac
        End Get
        Set(ByVal value As Integer)
            _soNgaySac = value
        End Set
    End Property
    Public Property SoNgaySuDung() As Integer
        Get
            Return _soNgaySuDung
        End Get
        Set(ByVal value As Integer)
            _soNgaySuDung = value
        End Set
    End Property
    Public Property CoThietBiDeBa() As Boolean
        Get
            Return _coThietBiDeBa
        End Get
        Set(ByVal value As Boolean)
            _coThietBiDeBa = value
        End Set
    End Property
#End Region

#Region "Xử lý Nền"

    Public Sub NhanGiaTri(ByVal rfunctionsolar As Integer, ByVal Rtongcongsautieuthutrongngay As Integer, ByVal Rtongcongsuatpin As Integer, ByVal Rtongcongsuattieuthu1gio As Integer, ByVal Rdungluongacquy12 As Integer, ByVal Rdungluongacquy24 As Integer, ByVal Rcongsuatbokichdien As Integer, ByVal Rdienapbokichdien As Integer, ByVal RCoThietBiDeBa As Integer, ByVal RSoNgaySac As Integer, ByVal RSoNgaySuDung As Integer)
        If Rtongcongsautieuthutrongngay > 0 And Rtongcongsuatpin > 0 And Rcongsuatbokichdien > 0 And Rdienapbokichdien > 0 Then
            FunctionSolar = rfunctionsolar
            TongCongSuatTieuThuTrongNgay = Rtongcongsautieuthutrongngay
            TongCongSuatTieuThu1Gio = Rtongcongsuattieuthu1gio
            TongCongSuatPin = Rtongcongsuatpin
            If FunctionSolar = 1 Then
                DungLuongAcquy12V = Rdungluongacquy12
                DungLuongAcquy24V = Rdungluongacquy24
            End If
            CoThietBiDeBa = RCoThietBiDeBa
            SoNgaySac = RSoNgaySac
            SoNgaySuDung = RSoNgaySuDung
            DienApDauRaBoKichDien = Rdienapbokichdien
            Status = True
        End If

    End Sub

    Private Sub LoadInfoFunction()
        If FunctionSolar = 0 Then
            lbNhuCauSuDung.Text = "Hòa lưới điện"
        ElseIf FunctionSolar = 1 Then
            lbNhuCauSuDung.Text = "Sạc - Dùng cho ngày cúp điện"
        End If
        lbTongCongSuatTieuThu.Text = TongCongSuatTieuThuTrongNgay.ToString()
        lbTongCongSuatPin.Text = TongCongSuatPin.ToString()
        lbDungLuongAcquy12.Text = DungLuongAcquy12V.ToString()
        lbDungLuongAcquy24.Text = DungLuongAcquy24V.ToString()
        lbCongSuatBoKichDien.Text = TongCongSuatTieuThuTrongNgay.ToString()
        lbDienApBoKichDien.Text = DienApDauRaBoKichDien.ToString()
    End Sub

    Private Function LoadHeThongPhuHop() As DataTable
        Dim dt As New DataTable
        For Each i As DataGridViewColumn In dgvHeThongPhuHop.Columns
            Dim dc As New DataColumn()
            dc.ColumnName = i.Name
            dt.Columns.Add(dc)
        Next

        Return TuVanBUS.ChonHeThong(dt, FunctionSolar, TongCongSuatPin, TongCongSuatTieuThu1Gio, DienApDauRaBoKichDien, CoThietBiDeBa, SoNgaySac, SoNgaySuDung)

        Return dt
    End Function

    Private Function SetupThePrinting() As Boolean
        If PrintDialog.ShowDialog() <> DialogResult.OK Then
            Return False
        End If

        PrintDocument.DocumentName = "Các Hệ Thống Điện Năng Lượng Mặt Trời Phù Hợp"
        PrintDocument.PrinterSettings = PrintDialog.PrinterSettings
        PrintDocument.DefaultPageSettings = PrintDialog.PrinterSettings.DefaultPageSettings
        PrintDocument.DefaultPageSettings.Margins = New Margins(40, 40, 40, 40)

        If MessageBox.Show("Canh chính giữa trang?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            MyDataGridViewPrinter = New DataGridViewPrinter(dgvHeThongPhuHop, PrintDocument, True, True, "Các Hệ Thống Điện Năng Lượng Mặt Trời Phù Hợp", New Font("Tahoma", 18, FontStyle.Bold, GraphicsUnit.Point), Color.Black, True)
        Else
            MyDataGridViewPrinter = New DataGridViewPrinter(dgvHeThongPhuHop, PrintDocument, False, True, "Customers", New Font("Tahoma", 18, FontStyle.Bold, GraphicsUnit.Point), Color.Black, True)
        End If
        Return True
    End Function

#End Region


#Region "Xử lý Control"
    Private Sub frmHeThongPhuHop_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Status = False Then
            MessageBox.Show("Chưa có thông tin kết quả tư vấn.", "Lổi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Me.Close()
        Else
            LoadInfoFunction()
        End If

        Dim dt = LoadHeThongPhuHop()
        dgvHeThongPhuHop.DataSource = dt


    End Sub
#End Region


    Private Sub btnIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIn.Click
        If SetupThePrinting() Then
            PrintDocument.Print()
        End If

    End Sub


    Private Sub dgvHeThongPhuHop_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvHeThongPhuHop.CellClick
        Dim TongGia As Int64 = Integer.Parse(dgvHeThongPhuHop.SelectedRows(0).Cells("GiaPin").Value.ToString()) + Integer.Parse(dgvHeThongPhuHop.SelectedRows(0).Cells("GiaMachDieuKhien").Value.ToString()) + Integer.Parse(dgvHeThongPhuHop.SelectedRows(0).Cells("GiaAcquy").Value.ToString()) + Integer.Parse(dgvHeThongPhuHop.SelectedRows(0).Cells("GiaBoKichDien").Value.ToString())
        Dim nfi As NumberFormatInfo = New CultureInfo("vi-VN", False).NumberFormat
        nfi.CurrencyDecimalDigits = 0
        lbTongTien.Text = TongGia.ToString("C", nfi)
    End Sub

    'Private Sub PrintDocument1_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument.PrintPage
    '    e.PageSettings.Landscape = True
    '    e.Graphics.DrawString(Label1.Text, Label1.Font, Brushes.Black, 100, 100)
    '    Dim data As New DataGridView()
    '    Dim width As Integer = 0
    '    Dim Height As Integer = 0
    '    For i = 0 To dgvHeThongPhuHop.Columns.Count - 1
    '        width += dgvHeThongPhuHop.Columns(i).Width
    '        data.Columns.Add(dgvHeThongPhuHop.Columns(i).Name, dgvHeThongPhuHop.Columns(i).HeaderText)
    '        data.Columns(i).DataPropertyName = dgvHeThongPhuHop.Columns(i).DataPropertyName
    '        data.Columns(i).Width = dgvHeThongPhuHop.Columns(i).Width
    '        data.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize
    '        data.RowHeadersVisible = False

    '    Next
    '    For i = 0 To dgvHeThongPhuHop.Rows.Count - 1
    '        Height += dgvHeThongPhuHop.Rows(i).Height
    '    Next

    '    data.Width = width
    '    data.Height = Height

    '    data.DataSource = dgvHeThongPhuHop.DataSource

    '    Dim bm As New Bitmap(width, Height)
    '    data.DrawToBitmap(bm, New Rectangle(0, 0, width, Height))
    '    e.Graphics.DrawImage(bm, 0, 100)
    'End Sub


    Private Sub btnXemTruoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXemTruoc.Click
        If SetupThePrinting() Then
            PrintPreviewDialog.Document = PrintDocument
            PrintPreviewDialog.ShowDialog()
        End If
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub PrintDocument_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument.PrintPage
        Dim more As Boolean = MyDataGridViewPrinter.DrawDataGridView(e.Graphics)
        If more = True Then
            e.HasMorePages = True
        End If
    End Sub

    Public Sub Export_To_Exel(ByVal _DataSet As DataTable, ByVal _List_Col As String, ByVal Path As String)
        '- _Dataset: lưu dữu liệu cần Export
        '- _List_Col: Danh sách tên cột hiển thị. Tên cột cách nhau dấu ','
        '- Path: Đường dẫn lưu file excel
        Dim col, row As Integer
        Dim arrListcol() As String
        arrListcol = _List_Col.Split(",")

        If _DataSet.Rows.Count <= 0 Then
            Exit Sub
        End If

        Dim Excel As Object = CreateObject("Excel.Application")
        If Excel Is Nothing Then
            MessageBox.Show("Để xuất ra Excel, máy bạn phải cài đặt bộ phần mềm Microsoft Office.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Return
        End If

        'Export to Excel process
        Try
            With Excel
                .SheetsInNewWorkbook = 1
                .Workbooks.Add()
                .Worksheets(1).Select()

                Dim i As Integer = 2
                .cells(1, 3).value = "Date:" + Format(Now, "MMM-dd-yyyy")
                .cells(2, i).value = "No"
                .cells(2, i).EntireRow.Font.Bold = True
                For col = 0 To arrListcol.Length - 1
                    i += 1
                    .cells(2, i).value = arrListcol(col).ToUpper()
                    .cells(2, i).EntireRow.Font.Name = "Times New Roman"
                    .cells(2, i).EntireRow.Font.Size = 10
                    .cells(2, i).EntireRow.Font.Bold = True
                Next
                i = 3
                Dim k As Integer = 3

                For col = 0 To _DataSet.Columns.Count - 1
                    i = 3
                    For row = 0 To _DataSet.Rows.Count - 1
                        .Cells(i, 2).Value = i - 2
                        .Cells(i, k).Value = _DataSet.Rows(row).ItemArray(col)
                        i += 1
                    Next
                    k += 1
                Next
                .Columns.AutoFit()
                .ActiveCell.Worksheet.SaveAs(Path)
            End With
            System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel)
            Excel = Nothing
            MessageBox.Show("Xuất dữ liệu thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)

            Dim proc As System.Diagnostics.Process
            For Each proc In System.Diagnostics.Process.GetProcessesByName("EXCEL")
                proc.Kill()
            Next

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btnExportExcel_Click(sender As Object, e As EventArgs) Handles btnExportExcel.Click
        Dim dt As DataTable = LoadHeThongPhuHop()
        Try
            SaveFileExcel.Filter = "Excel (*.xls)|*.xls"
            SaveFileExcel.ShowDialog()
            Dim File_name = SaveFileExcel.FileName
            Export_To_Exel(dt, "Tên Pin,Công suất Pin,Số lượng pin,Giá pin,Tên mạch điều khiển,Dòng điện mạch,Giá mạch,Tên ÁcQuy,Dung lượng,Số lượng ÁcQuy,Giá ÁcQuy,Tên BKD,Công suất BKD,Điện áp BKD,Giá BKD", File_name)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop)
        End Try
    End Sub
End Class
